﻿using System;

// 学生类
class Student
{
    // 私有字段
    private string _name;
    private int _age;
    private string _studentId;
    private double[] _scores;
    
    // 公共属性
    public string Name 
    { 
        get { return _name; } 
        set { _name = value; } 
    }
    
    public int Age 
    { 
        get { return _age; } 
        set 
        { 
            if (value >= 0)
                _age = value;
            else
                throw new ArgumentException("年龄不能为负数");
        } 
    }
    
    public string StudentId 
    { 
        get { return _studentId; } 
        set { _studentId = value; } 
    }
    
    // 构造函数
    public Student(string name, int age, string studentId)
    {
        _name = name;
        Age = age; // 使用属性的setter，会进行验证
        _studentId = studentId;
        _scores = new double[0];
    }
    
    // 添加成绩
    public void AddScore(double score)
    {
        if (score < 0 || score > 100)
            throw new ArgumentException("成绩必须在0到100之间");
        
        // 扩大数组并添加新成绩
        Array.Resize(ref _scores, _scores.Length + 1);
        _scores[_scores.Length - 1] = score;
    }
    
    // 计算平均成绩
    public double CalculateAverageScore()
    {
        if (_scores.Length == 0)
            return 0;
        
        double sum = 0;
        foreach (double score in _scores)
        {
            sum += score;
        }
        return sum / _scores.Length;
    }
    
    // 获取学生信息
    public string GetStudentInfo()
    {
        return $"姓名：{_name}\n年龄：{_age}\n学号：{_studentId}\n平均成绩：{CalculateAverageScore():F2}";
    }
}

// 主程序类
class StudentProgram
{
    // 程序入口点
    static void Main(string[] args)
    {
        Console.WriteLine("学生类程序");
        Console.WriteLine("==========");
        
        try
        {
            // 创建学生对象
            Student student = new Student("Darren", 20, "202420244606");
            
            // 添加成绩
            student.AddScore(85);
            student.AddScore(92);
            student.AddScore(78);
            
            // 显示学生信息
            Console.WriteLine("学生信息：");
            Console.WriteLine(student.GetStudentInfo());
            
            // 修改学生信息
            student.Name = "孙永康";
            student.Age = 20;
            student.AddScore(95);
            
            Console.WriteLine("\n修改后的学生信息：");
            Console.WriteLine(student.GetStudentInfo());
        }
        catch (Exception ex)
        {
            Console.WriteLine("发生错误：" + ex.Message);
        }
        
        Console.WriteLine("\n程序结束，按任意键退出...");
        Console.ReadKey();
    }
}
